package com.cookbook.controller;

import com.cookbook.RequestAuthority;
import com.cookbook.Result;
import com.cookbook.database.model.Comment;
import com.cookbook.database.model.Reply;
import com.cookbook.database.model.User;
import com.cookbook.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 毛川
 */
@Controller
@RequestMapping("/comment")
@SessionAttributes("user")
public class CommentController {

    @Autowired
    CommentService commentService;

    @Transactional
    @RequestMapping(value = "/comments", method = RequestMethod.GET)
    @ResponseBody
    public String comments(
            @RequestParam Integer shareId,
            @RequestParam Integer page,
            @RequestParam Integer limit) {

        if (page < 0 || limit < 0) {
            return Result.format(Result.PARAMETER_INVALID, "非法参数");
        }

        try {
            List<Comment> comments = commentService.commentsWithOffset(shareId, page, limit);
            return Result.success(comments);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.format(Result.INTERNAL_ERROR, "内部错误");
        }
    }

    @RequestAuthority
    @Transactional
    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    @ResponseBody
    public String insertComment(
            @ModelAttribute User user,
            @RequestParam Integer shareId,
            @RequestParam String content) {
        if (user == null)
            return Result.format(Result.INTERNAL_ERROR, "内部错误");

        if (shareId < 0 || content.isEmpty() || content.length() > 255)
            return Result.format(Result.PARAMETER_INVALID, "参数错误");

        try {
            Comment comment = new Comment();
            comment.setShareId(shareId);
            comment.setUser(user);
            comment.setContent(content);
            commentService.insert(comment);
            return Result.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.format(Result.INTERNAL_ERROR, "内部错误");
        }
    }

    @Transactional
    @RequestMapping(value = "/replies", method = RequestMethod.GET)
    @ResponseBody
    public String replies(
            @RequestParam Integer commentId,
            @RequestParam Integer page,
            @RequestParam Integer limit) {
        if (commentId < 0 || page < 0 || limit < 0)
            return Result.format(Result.PARAMETER_INVALID, "参数非法");

        try {
            List<Reply> replies = commentService.repliesWithOffset(commentId, page, limit);
            return Result.success(replies);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.format(Result.INTERNAL_ERROR, "内部错误");
        }
    }

    @RequestAuthority
    @Transactional
    @RequestMapping(value = "/reply", method = RequestMethod.POST)
    @ResponseBody
    public String insertReply(
            @ModelAttribute User user,
            @RequestParam Integer commentId,
            @RequestParam(required = false) Integer replyTo,
            @RequestParam String content) {
        if (user == null)
            return Result.format(Result.INTERNAL_ERROR, "内部错误");

        if (commentId < 0 || (replyTo != null && replyTo < 0)  ||
                content.isEmpty() || content.length() > 255)
            return Result.format(Result.PARAMETER_INVALID, "参数错误");

        try {
            User replyToUser = new User();
            replyToUser.setUserId(replyTo);
            Reply reply = new Reply();
            reply.setCommentId(commentId);
            reply.setAuthor(user);
            reply.setReplyTo(replyToUser);
            reply.setContent(content);
            commentService.insert(reply);
            return Result.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.format(Result.INTERNAL_ERROR, "内部错误");
        }
    }

}
